package com.itcast.tcp.server.decoder_tcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @program: bio_tcp
 * @description: ReplayingDecoder也是继承了ByteToMessageDecoder进⾏的扩展 ,
 * 是通过先读一个int,判断后面的int长度的数据才是真实需要读取的数据,tcp的粘包和拆包的Decoder类
 * tcp基于流,只能保证顺序,不能保证数据每片大小,使用ReplayingDecoder,就可以保证读到的长度,做好切割
 * @author: zhanghz001
 * @create: 2021-02-23 09:49
 **/
public class IntegerHeaderFrameDecoder extends ReplayingDecoder<Void> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext,
                          ByteBuf byteBuf, List<Object> list) throws Exception {
        list.add(byteBuf.readBytes(byteBuf.readInt()));
    }
}
